﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.admin.order
{
    public partial class daili_shop : MxWeiXinPF.Web.UI.ManagePage
    {
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        protected int payment_status;
        protected MxWeiXinPF.Model.manager admin_info;
        protected int status;
        protected int totalCount;
        protected string starttime;
        protected string endtime;
        protected int selectshopid;
        protected decimal sumamount = 0.0m;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.status = MXRequest.GetQueryInt("status");
            this.payment_status = MXRequest.GetQueryInt("payment_status");
            this.selectshopid = MXRequest.GetQueryInt("selectshopid");
            this.keywords = MXRequest.GetQueryString("keywords");
            this.pageSize = this.GetPageSize(10);
            this.starttime = MXRequest.GetQueryString("start");
            this.endtime = MXRequest.GetQueryString("end");
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("daili_shop", MXEnums.ActionEnum.View.ToString());
                this.admin_info = base.GetAdminInfo();

                this.RptBind(" " + this.CombSqlTxt(this.status, this.payment_status, this.selectshopid, this.keywords), "shopid asc,confirm_time desc");
                if (string.IsNullOrEmpty(this.starttime))
                {
                    txtStart.Text = DateTime.Now.AddDays(-7).ToShortDateString();
                }
                else
                {
                    this.txtStart.Text = starttime;
                }
                if (string.IsNullOrEmpty(endtime))
                {
                    txtEnd.Text = DateTime.Now.ToShortDateString();
                }
                else
                {
                    this.txtEnd.Text = endtime;
                }
            }
        }

        protected string CombSqlTxt(int _status, int _payment_status, int selectshopid, string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            if (_status > 0)
            {
                builder.Append(" and status=" + _status);
            }
            if (_payment_status > 0)
            {
                builder.Append(" and payment_status=" + _payment_status);
            }
            if (selectshopid > 0)
            {
                builder.Append(" and shopid=" + selectshopid);
            }
            builder.Append(" and payment_id=3 ");
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and (order_no like '%" + _keywords + "%' or user_name like '%" + _keywords + "%' or accept_name like '%" + _keywords + "%')");
            }


            if (string.IsNullOrEmpty(starttime))
            {
                builder.AppendFormat(" and complete_time >= '" + DateTime.Now.AddDays(-7).ToShortDateString() + "' ");
            }
            else
            {
                builder.AppendFormat(" and complete_time >= '" + starttime + "' ");
            }
            if (string.IsNullOrEmpty(endtime))
            {
                builder.AppendFormat(" and complete_time <= '" + DateTime.Now.ToShortDateString() + "' ");
            }
            else
            {
                builder.AppendFormat(" and complete_time <= '" + endtime + "' ");
            }
            return builder.ToString();
        }

        protected void ddlPaymentStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}&start={4}&end={5}", new string[] { this.status.ToString(), this.ddlPaymentStatus.SelectedValue, this.selectshopid.ToString(), this.keywords, this.starttime, this.endtime }));
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}", new string[] { this.ddlStatus.SelectedValue, this.payment_status.ToString(), this.selectshopid.ToString(), this.keywords, this.starttime, this.endtime }));
        }
        protected void ddlShops_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}&start={4}&end={5}", new string[] { this.ddlStatus.SelectedValue, this.payment_status.ToString(), this.ddlShops.SelectedValue, this.keywords, this.starttime, this.endtime }));
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}&start={4}&end={5}", new string[] { this.status.ToString(), this.payment_status.ToString(), this.selectshopid.ToString(), this.txtKeywords.Text, this.starttime, this.endtime }));
        }
        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("order_daili_shop_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        private void RptBind(string _strWhere, string _orderby)
        {
            MxWeiXinPF.Model.manager weixin = base.GetAdminInfo();
            this.page = MXRequest.GetQueryInt("page", 1);
            if (this.status > 0)
            {
                this.ddlStatus.SelectedValue = this.status.ToString();
            }
            if (this.payment_status > 0)
            {
                this.ddlPaymentStatus.SelectedValue = this.payment_status.ToString();
            }
            if (this.selectshopid > 0)
            {
                this.ddlShops.SelectedValue = this.selectshopid.ToString();
            }
            else
            {
                this.ddlShops.SelectedValue = "0";
            }
            this.txtKeywords.Text = this.keywords;
            BLL.orders bll = new BLL.orders();
            DataSet set = new DataSet();
            DataSet ds2 = new DataSet();
            if (weixin.agentLevel == 0)
            {
               set = bll.GetList(weixin.id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);      
            }
            else
            {
                if (weixin.agentLevel == 1)
                {
                    set = bll.GetListByCounty(weixin.id, string.IsNullOrEmpty(weixin.county) ? 0 : int.Parse(weixin.county), this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                    ds2 = bll.GetListByCountySum(weixin.id, string.IsNullOrEmpty(weixin.county) ? 0 : int.Parse(weixin.county), _strWhere);
                }
                else if (weixin.agentLevel == 2)
                {
                    set = bll.GetListByCity(weixin.id, string.IsNullOrEmpty(weixin.city) ? 0 : int.Parse(weixin.city), this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                    ds2 = bll.GetListByCitySum(weixin.id, string.IsNullOrEmpty(weixin.city) ? 0 : int.Parse(weixin.city), _strWhere);
                }
                else if (weixin.agentLevel == 3)
                {
                    set = bll.GetListByProvince(weixin.id, string.IsNullOrEmpty(weixin.province) ? 0 : int.Parse(weixin.province), this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
                    ds2 = bll.GetListByProvinceSum(weixin.id, string.IsNullOrEmpty(weixin.province) ? 0 : int.Parse(weixin.province), _strWhere);
                }
                else
                {
                    return;
                }
            }

            if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    row["statusName"] = this.GetOrderStatus(MyCommFun.Obj2Int(row["id"]), MyCommFun.Obj2Int(row["status"]), MyCommFun.Obj2Int(row["payment_status"]), MyCommFun.Obj2Int(row["express_status"]));
                }
            }
            this.rptList.DataSource = set;
            this.rptList.DataBind();
            this.TreeBind(set);
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}&page={4}&start={5}&end={6}", new string[] { this.status.ToString(), this.payment_status.ToString(), this.selectshopid.ToString(), this.keywords, "__id__",this.starttime,this.endtime });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
            if (ds2 != null && ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                if (ds2.Tables[0].Rows[0][0]!= null && ds2.Tables[0].Rows[0][0].ToString() != "")
                {
                    sumamount = Convert.ToDecimal(ds2.Tables[0].Rows[0][0].ToString());
                    this.lbSum.Text = sumamount.ToString() + "元";
                }
            }
            this.lbSum.Text = sumamount.ToString() + "元";
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("order_daili_shop_page_size", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("daili_shop.aspx", "status={0}&payment_status={1}&selectshopid={2}&keywords={3}&start={4}&end={5}", new string[] { this.status.ToString(), this.payment_status.ToString(), this.selectshopid.ToString(), this.keywords, this.starttime, this.endtime }));
        }

        protected string GetOrderStatus(int _id, int status, int payment_status, int express_status)
        {
            string str = string.Empty;
            switch (status)
            {
                case 1:
                    if (payment_status <= 0)
                    {
                        return "待确认";
                    }
                    return "待付款";

                case 2:
                    if (express_status <= 1)
                    {
                        return "待发货";
                    }
                    return "已发货";

                case 3:
                    return "交易完成";

                case 4:
                    return "已取消";

                case 5:
                    return "已作废";
            }
            return str;
        }

        private void TreeBind(DataSet set)
        {
            try
            {
                this.ddlShops.Items.Clear();
                this.ddlShops.Items.Add(new ListItem("所有店铺", "0"));
                var query = from t in set.Tables[0].AsEnumerable()
                            group t by new { t1 = t.Field<int>("shopid"), t2 = t.Field<string>("shopname") } into m
                            select new
                            {
                                shopid = m.Key.t1,
                                shopname = m.Key.t2
                            };
                if (query.ToList().Count > 0)
                {
                    query.ToList().ForEach(q =>
                    {
                        this.ddlShops.Items.Add(new ListItem(q.shopname,q.shopid.ToString()));
                        if (q.shopid == this.selectshopid)
                        {
                            this.ddlShops.SelectedValue = q.shopid.ToString();
                        }
                    });
                } 
            }
            catch (Exception ex)
            {

            }
        }

        public string GetShopName(int shopid)
        {
            Model.manager mm = new BLL.manager().GetModel(shopid);
            if (mm != null)
            {
                return mm.real_name;
            }
            return "";
        }
    }
}